package com.game.raceroom.player.disp
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import com.util.SwfUtil;
	import com.game.raceroom.player.Player;
	
	import mx.core.UIComponent;
	
	public class ActorDisplayer extends Displayer
	{
   
        public var nameBar:TextField;
        public var comboBar:TextField;
        public var readyBar:TextField;
        private var userName:String;
        private var map_width,map_height,map_x,map_y:int;
        private var actor:Player;
        private var shape:Shape=new Shape();
        private var bloodBar:Shape=new Shape();
        private var x1,x2,y1,y2:int;
        public var mc:MovieClip=new MovieClip;
        public var strickmc:MovieClip=new MovieClip;
        public var eftLayer:UIComponent=new UIComponent;
        private var format:TextFormat= new TextFormat();
        
 	 	 private var format1:TextFormat= new TextFormat();			
 			public var myFilter:GlowFilter = 
		    new GlowFilter(0, 0.9, 2,2, 10, 1, false, false); 
//	 		public var crashFilter1:GlowFilter = 
//		    new GlowFilter(0xFFFFFF, 0.7, 9,9, 10, 2, false, false);  
//		 	public var crashFilter2:GlowFilter = 
//		    new GlowFilter(0xFFFFFF, 0.7, 4,4, 10, 2, false, false);  
  
		public function ActorDisplayer(gameCanvas:UIComponent):void
		{
			super(gameCanvas);
		}
		public function setCombo(combo:int){
			if(combo>0){
				this.comboBar.text="Combo "+combo;
			}else{
				this.comboBar.text="";
			}
		}
        public function setReady(ready:Boolean){
        	if(ready){
                this.alpha=1;
        		readyBar.visible=false;
        	}else{
        		this.alpha=0.6;
        		readyBar.visible=true;
        	}
        }
		public function setActorInfo(actor:Player){
			this.actor=actor;
			userName=actor.userInfo.userNickName;
			nameBar=new TextField();
			
			readyBar=new TextField();
			format.size=13;
		    format.bold=true;
			format1.size=50;
		    format1.bold=true;			
			nameBar.text=userName;
			nameBar.selectable=false;
			nameBar.textColor=0xFFFFFF;
			nameBar.setTextFormat(format);
			nameBar.filters=[this.myFilter];
			if(this.actor.isActor){
	            comboBar=new TextField();
	            comboBar.text="";
				comboBar.selectable=false;
				comboBar.textColor=0x4444FF;
				comboBar.setTextFormat(format);
				comboBar.filters=[this.myFilter];
			}
			 
			readyBar.text="等待";
			readyBar.textColor=0x00FF00;
			readyBar.selectable=false;
			readyBar.setTextFormat(format1);
			readyBar.filters=[this.myFilter];	
		}
		public function setTeamColor(c:int){
			  nameBar.textColor=c;
		}
//		public var afterCrash:int=0;
//		public function addCrashFilter():void{
//			afterCrash=4;
//			mc.filters=[this.crashFilter1];
//		}
        public function showConflictBox(x:int,y:int):void{
        	x1=x-12;
        	x2=x+12;
        	y1=y-15;
        	y2=y+15;
        	shape.graphics.clear();
            shape.graphics.lineStyle(2,0x0000FF,50);
			// shape.g raphics.moveTo(x1,y1); 
			// shape.graphics.lineTo(x2,y1); 
			// shape.graphics.lineTo(x2,y2);  
			// shape.graphics.lineTo(x1,y2);  
			// shape.graphics.lineTo(x1,y1); 
 
  			shape.graphics.drawCircle(x,y,actor.carInfo.confRadius); 
  			//shape.graphics.drawRect(mc.x,mc.y,mc.width,mc.height);
 
        	shape.x=0;
		    shape.y=0;
        	//actorImage.
        }
        public function refreshBloodBar (max:int,now:int):void{
        	 bloodBar.graphics.clear();
        	//
        	//bloodBar.graphics.drawRect(1,0,82,10);
        	//bloodBar.graphics.drawRect(0,1,84,8);

        	var wid:Number=70/max*now;
        	bloodBar.graphics.beginFill(0xBB0000);
        	bloodBar.graphics.drawRect(1,1,wid,6);
        	bloodBar.graphics.endFill();
        	bloodBar.graphics.beginFill(0xDD0000);
        	
        	bloodBar.graphics.drawRect(1,2,wid,4);
        	bloodBar.graphics.endFill();
        	bloodBar.graphics.beginFill(0xFF0000);
        	
        	bloodBar.graphics.drawRect(1,3,wid,2);
        	bloodBar.graphics.endFill();
        	bloodBar.graphics.lineStyle(1,0xDDDDDD);
        	bloodBar.graphics.drawRect(0,0,72,8);

        }
        public function initBloodBar(max:int,now:int):void{
        	refreshBloodBar(max,now);
        	
        }

		//转方向
		private var angle:Number=-1;
       
		public function setAttack(a:int):void{
			if(a>0){
				nameBar.textColor=0xFF0000;
			}else{
				nameBar.textColor=0xFFFFFF;
			}
		}
		var aaa:Number=0;
		var ro:int=0;

	    
	    private function rotateStrick(){
		   		//  if(strickmc.visible){
		 			 var mt:Matrix=strickmc.transform.matrix; 
		
					 mt.a=1;
					 mt.b=0;
					 mt.c=0;
					 mt.d=1;
					 mt.tx=this.strickWidth;
					 mt.ty=this.strickHeight;
				  	 mt.rotate(Math.PI*2*(((ro+270)%360)/360));
				  	 eftLayer.x=mc.x+mc.width/2;
				     eftLayer.y=mc.y+mc.height/2;
				   	 strickmc.transform.matrix=mt;
				   	 strickmc.visible=true;
				     strickmc.alpha=1;
				   	 strickmc.play();
				     
				//  }
	    }	    
		public function turnActor(dir:int):void{
           ro=dir;
		   var temp:int=Math.floor(dir/15)%24+1;
		   if(temp!=angle){
		   		angle=temp;
		   		  mc.gotoAndStop(angle);
                  onStrick=false;
		   }
		}
		//var disp_x,disp_y,disp_width,disp_height:int;
		public var c_x,c_y:Number;
             var mc_width:int;
            var mc_height:int;
            var mcx:Number;
            var mcy:Number;
            var mcx1:Number;
            var mcy1:Number;
            var scale:Number=1;
        public function setScale(s:Number){
        	scale=s;
        }
        private var onStrick:Boolean=false;
        private var strickTime:int=0;
        public function setStrick(){
        	onStrick=true;
        	strickTime=8;
        	rotateStrick();
        }
        var frame:int=0;
		public function drawActor(x:Number,y:Number):void{

//	       if(afterCrash>0){
//	       	  afterCrash--;
//	       	  if(afterCrash<3){
//	       	  	 mc.filters=[this.crashFilter2];
//	       	  }
//	       	  if(afterCrash==0){
//	       	  	 mc.filters=null;
//	       	  }
//	       }
		    x=Math.round(x);
            y=Math.round(y);
            if(mc.scaleX!=scale){
            	mc.scaleX=scale;
            	mc.scaleY=scale;
            }
            mc_width=mc.width/2;
            mc_height=mc.height/2;
            mcx=x-mc_width;
            mcy=y-mc_height;
     
            mcx1=x+mc_width;
            mcy1=y+mc_height;
            if(mcx>actor._parent.mapContrl.mapDisp.screen_end_x
 			 ||mcy>actor._parent.mapContrl.mapDisp.screen_end_y
 			 ||mcx1<actor._parent.mapContrl.mapDisp.screen_start_x
 			 ||mcy1<actor._parent.mapContrl.mapDisp.screen_start_y){
            	mc.visible=false;
            }
            else{
            	mc.visible=true;
            }           

			mc.x=mcx-actor._parent.mapContrl.mapDisp.screen_start_x;
			mc.y=mcy-actor._parent.mapContrl.mapDisp.screen_start_y;
			 if(onStrick ){
				  eftLayer.x=mc.x+mc_width;
				  eftLayer.y=mc.y+mc_height;
				  strickmc.alpha-=0.04;
			      this.strickTime--;
			      if(strickTime<0){
			       	onStrick=false;
			       	
			      }
			}else{
				 strickmc.stop();
				 strickmc.visible=false;	
			}
		    nameBar.x=mc.x+mc.width/2-36;
		    nameBar.y=mc.y-28;
		    if(this.actor.isActor){
			    this.comboBar.x=nameBar.x;
			    this.comboBar.y=nameBar.y-15;
		    }
		    bloodBar.x=nameBar.x;
		    bloodBar.y=mc.y-10;
		    if(this.readyBar.visible){
		    	readyBar.x=(x-readyBar.width/2)-actor._parent.mapContrl.mapDisp.screen_start_x;
		    	readyBar.y=(y-readyBar.height/2)-actor._parent.mapContrl.mapDisp.screen_start_y;
		    }
		   //showConflictBox(x-actor._parent.mapContrl.mapDisp.screen_start_x,y-actor._parent.mapContrl.mapDisp.screen_start_y);
		}
 
 
		public function update():void{
			
			map_x=-0;
			map_y=-0;
 
		}
		public function drawFirst(id:int):void{
             loadCarMovie(id);
			 
		}
		var destroyed:Boolean=false;
		public function destory():void{
			if(destroyed)return;
			destroyed=true;
			gameCanvas.removeChild(this);
			removeChild(mc);
			removeChild(nameBar);
			removeChild(shape);
			removeChild(bloodBar);
		}
 
		public function hide(alpha:Number):void{
			this.alpha=alpha;

		}
 	
		private var myLoader:Loader;
		private var myLoader1:Loader;
		public function loadCarMovie(id:int):void{
			myLoader=new Loader();
		    myLoader.contentLoaderInfo.addEventListener(
  	        Event.COMPLETE,function (e:Event):void{initCarOk(e)});
            myLoader.loadBytes(SwfUtil.getSwfStream(id));
            

		}
		private function loadEftMovie(){
			myLoader1=new Loader();
		    myLoader1.contentLoaderInfo.addEventListener(
  	        Event.COMPLETE,function (e:Event):void{initStrickOk(e)});
            myLoader1.loadBytes(SwfUtil.getEffectSwfStream(SwfUtil.EFT_NAME_STRICK));	
		}	
		var mcs:Array;	
//		var fire:Fire=new Fire;	
        public function initCarOk(e:Event):void{
        	this.visible=false;
            mc = myLoader.content as MovieClip;
            mc.stop();
           loadEftMovie();
			gameCanvas.addChild(this);
		    addChild(mc);
			addChild(nameBar);
			addChild(shape);
			addChild(bloodBar);
			addChild(readyBar);
			if(this.actor.isActor){
				addChild(comboBar);
			}
            actor.prepared();
           // addChild(fire);
        } 
        var strickWidth:int;
        var strickHeight:int;
        public function initStrickOk(e:Event):void{
            this.strickmc=myLoader1.content as MovieClip;
           // addChild(fire);
           strickmc.visible=false;
 
          // strickmc.x=mc.x;
          // strickmc.y=mc.y;
           //strickmc.stop();
           strickWidth=-strickmc.width/2;
           strickHeight=-strickmc.height/2;
          strickmc.x=strickWidth;
          strickmc.y=strickHeight;
           this.addChild(eftLayer);
           eftLayer.addChild(this.strickmc);
        }  

	}
}